// pages/user/institute/institute.js
import {
  getMyCompanyList
} from '../../../api/institute.js'
import {
  removeBadge
} from '../../../api/other.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperIndex: 0,
    badgePass: [],
    badgePending: [],
    badgeFail: [],
    passList: [],
    rejectList: [],
    waitList: [],
    pageNo: [1, 1, 1],
    rowCount: [0, 0, 0],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.userInfo)
    this.getData(1)
    this.getData(0)
    this.getData(2)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const {
      school
    } = getApp().globalData.unRead;
    console.log(school)
    this.setData({
      badgePass: school.pass,
      badgePending: school.pending,
      badgeFail: school.fail,
    })
  },

  getData(type) { //0-待审核1-审核通过2-驳回
    getMyCompanyList(type, this.data.pageNo[this.data.swiperIndex]).then(res => {
      if (type === 0) {
        // 判断是否显示红点
        res.list = res.list.map(item => {
          if (this.data.badgePending.includes(item.companyApplyId)) {
            item.badge = true;
          } else {
            item.badge = false;
          }
          return item;
        })
        this.setData({
          waitList: this.data.waitList.concat(res.list),
          rowCount: [this.data.rowCount[0], res.rowCount, this.data.rowCount[2]]
        })
      } else if (type === 1) {
        // 判断是否显示红点
        res.list = res.list.map(item => {
          if (this.data.badgePass.includes(item.companyApplyId)) {
            item.badge = true;
          } else {
            item.badge = false;
          }
          return item;
        })
        this.setData({
          passList: this.data.passList.concat(res.list),
          rowCount: [res.rowCount, this.data.rowCount[1], this.data.rowCount[2]]
        })
        console.log(this.data.passList)
      } else if (type === 2) {
        // 判断是否显示红点
        res.list = res.list.map(item => {
          if (this.data.badgeFail.includes(item.companyApplyId)) {
            item.badge = true;
          } else {
            item.badge = false;
          }
          return item;
        })
        this.setData({
          rejectList: this.data.rejectList.concat(res.list),
          rowCount: [this.data.rowCount[0], this.data.rowCount[1], res.rowCount]
        })
      }
    })
  },
  jumpToDetails(e) {
    const {
      id,
      applyId,
    } = e.currentTarget.dataset;
    this.clearBadge(applyId);
    wx.navigateTo({
      url: '/pages/institute/details/details?id=' + id,
    })
  },

  /**
   * 消除红点
   * @param {number} id 商学院申请编号
   */
  clearBadge(id) {
    if (this.data.swiperIndex === 0) { // 已通过
      getApp().globalData.unRead.school.pass = this.data.badgePass.filter(item => item !== id);
      let list = this.data.passList.map(item => {
        if (item.id === id) {
          item.badge = false;
        }
        return item;
      })
      removeBadge(2, id);
      this.setData({
        passList: list
      })
    } else if (this.data.swiperIndex === 1) { // 待审核
      getApp().globalData.unRead.school.pending = this.data.badgePending.filter(item => item !== id);
      let list = this.data.waitList.map(item => {
        if (item.id === id) {
          item.badge = false;
        }
        return item;
      })
      removeBadge(4, id);
      this.setData({
        waitList: list
      })
    } else if (this.data.swiperIndex === 2) { // 未通过
      getApp().globalData.unRead.school.fail = this.data.badgeFail.filter(item => item !== id);
      let list = this.data.rejectList.map(item => {
        if (item.id === id) {
          item.badge = false;
        }
        return item;
      })
      removeBadge(3, id);
      this.setData({
        rejectList: list
      })
    }
  },
  /**
   * tabs 切换
   */
  handleTabsChange(e) {
    const {
      key
    } = e.detail;
    const swiperIndex = parseInt(key);
    this.setData({
      key,
      swiperIndex,
    })
    if (this.data.swiperIndex === 0) {
      this.data.pageNo[0] = 1;
      this.data.passList = [];
      this.getData(1)
    } else if (this.data.swiperIndex === 1) {
      this.data.waitList = [];
      this.data.pageNo[1] = 1
      this.getData(0)

    } else if (this.data.swiperIndex === 2) {
      this.data.rejectList = [];
      this.data.pageNo[2] = 1
      this.getData(2)
    }
  },

  /**
   * swiper 切换
   */
  handleSwiperChange(e) {
    const {
      current
    } = e.detail
    const key = current.toString();
    this.setData({
      key,
      swiperIndex: current,
    })

  },
  handleScrollBottom(e) {
    const {
      index
    } = e.currentTarget.dataset;
    if (this.data.swiperIndex === 0 && this.data.rowCount[0] > this.data.passList.length) {
      this.data.pageNo[0] += 1
      this.getData(1)
    } else if (this.data.swiperIndex === 1 && this.data.rowCount[1] > this.data.waitList.length) {

      this.data.pageNo[1] += 1
      this.getData(0)

    } else if (this.data.swiperIndex === 2 && this.data.rowCount[2] > this.data.rejectList.length) {
      this.data.pageNo[2] += 1
      this.getData(2)

    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})